#include <HardwareSerial.h>
#include "Controls.h"
#include "Controller.h"

Controls::Controls() : illumination(NULL) {
}

const error_t Controls::begin() {
    pinMode(PIN_CONTROL_MODE, INPUT);

    return NO_ERROR;
}

error_t Controls::attachIllumination(Illumination &illumination) {
    this->illumination = &illumination;

    illuminationBounce.attach(PIN_CONTROL_MODE);
    illuminationBounce.interval(500);

    return NO_ERROR;
}

const error_t Controls::tick(const tick_t ticks, const time_t time) {
    if (illumination != NULL) {
        illuminationBounce.update();

        if (illuminationBounce.rose()) { // state changed - next mode
            const Mode mode = illumination->getMode();
            switch (mode) {
                case AUTO:
                    illumination->setMode(BLAZE);
                    break;
                case BLAZE:
                    illumination->setMode(DEMO);
                    break;
                case DEMO:
                    illumination->setMode(AUTO);
                    break;
                default:
                    illumination->setMode(AUTO);
                    break;
            }
        }
    }
    return NO_ERROR;
}
